package com.scale.service.character.service.impl;

import com.example.character.entity.AiCharacters;
import com.example.character.vo.CharacterStatusVO;
import com.scale.service.character.mapper.AiCharactersMapper;
import com.scale.service.character.service.AiCharactersService;
import com.scale.service.conversation.service.ConversationsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * AI角色表 服务实现类
 * </p>
 *
 * @author crp
 * @since 2025-09-24
 */
@Service
public class AiCharactersServiceImpl extends ServiceImpl<AiCharactersMapper, AiCharacters> implements AiCharactersService {

    @Autowired
    private ConversationsService conversationsService;

    @Override
    public List<AiCharacters> getCharacterList(String characterType, Boolean isActive) {
        QueryWrapper<AiCharacters> queryWrapper = new QueryWrapper<>();
        
        if (characterType != null && !characterType.isEmpty()) {
            queryWrapper.eq("character_type", characterType);
        }
        
        if (isActive != null) {
            queryWrapper.eq("is_active", isActive);
        }
        
        queryWrapper.orderByAsc("sort_order");
        
        return list(queryWrapper);
    }

    @Override
    public CharacterStatusVO getCharacterStatus(Long characterId) {
        AiCharacters character = getById(characterId);
        if (character == null) {
            return null;
        }
        
        CharacterStatusVO statusVO = new CharacterStatusVO();
        statusVO.setId(character.getId());
        statusVO.setName(character.getName());
        
        // 设置状态（这里可以根据实际业务逻辑来判断）
        statusVO.setStatus(character.getIsActive() ? "online" : "offline");
        
        // 获取当前活跃对话数
        Integer activeConversations = conversationsService.getActiveConversationCount(characterId);
        statusVO.setActiveConversations(activeConversations);
        
        // 获取今日对话总数
        Integer todayConversations = conversationsService.getTodayConversationCount(characterId);
        statusVO.setTodayConversations(todayConversations);
        
        // 设置平均响应时间（这里可以从统计表获取，暂时设置默认值）
        statusVO.setAvgResponseTime(1500);
        
        // 设置最后活动时间
        statusVO.setLastActiveTime(LocalDateTime.now());
        
        return statusVO;
    }
}
